package internact;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings({"all"})

public class UserPanel extends JPanel {
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton registerButton;
    private JButton modifyButton;
    private JButton logoutButton;
    private UserManager userManager;

    public UserPanel(UserManager userManager) {
        this.userManager = userManager;

        setLayout(new GridLayout(5, 2));

        usernameLabel = new JLabel("用户名：");
        add(usernameLabel);
        usernameField = new JTextField();
        add(usernameField);

        passwordLabel = new JLabel("密码：");
        add(passwordLabel);
        passwordField = new JPasswordField();
        add(passwordField);

        loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (userManager.login(username, password)) {
                    JOptionPane.showMessageDialog(UserPanel.this, "登录成功！");
                } else {
                    JOptionPane.showMessageDialog(UserPanel.this, "登录失败！");
                }
            }
        });
        add(loginButton);

        registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (userManager.register(username, password)) {
                    JOptionPane.showMessageDialog(UserPanel.this, "注册成功！");
                } else {
                    JOptionPane.showMessageDialog(UserPanel.this, "注册失败！");
                }
            }
        });
        add(registerButton);

        modifyButton = new JButton("修改");
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (userManager.modify(username, password)) {
                    JOptionPane.showMessageDialog(UserPanel.this, "修改成功！");
                } else {
                    JOptionPane.showMessageDialog(UserPanel.this, "修改失败！");
                }
            }
        });
        add(modifyButton);

        logoutButton = new JButton("退出");
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userManager.logout();
                JOptionPane.showMessageDialog(UserPanel.this, "退出成功！");
            }
        });
        add(logoutButton);
    }
}
